package com.azzcs;

import com.azzcs.dao.UserMapper;
import com.azzcs.entity.User;
import com.azzcs.parse.Resource;
import com.azzcs.session.SqlSession;
import com.azzcs.session.SqlSessionFactory;
import com.azzcs.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;

/**
 * @Author: wzg
 * @Date: 2020/12/6 上午10:36
 */
public class Main {
    private SqlSession sqlSession;
    @Before
    public void before() throws Exception {
        InputStream input = Resource.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(input);
        sqlSession = sqlSessionFactory.openSession();
    }

    @Test
    public void sqlSessionTest() throws Exception {
        User parameter =  User.builder().id(1).build();

        sqlSession.delete("com.azzcs.dao.UserMapper.delete", parameter);
        User user = sqlSession.selectOne("com.azzcs.dao.UserMapper.selectOne", parameter);
        System.out.println("开始前User:"+user);


        parameter.setUsername("张三");
        int insert = sqlSession.insert("com.azzcs.dao.UserMapper.insert", parameter);
        user = sqlSession.selectOne("com.azzcs.dao.UserMapper.selectOne", parameter);
        System.out.println(String.format("执行插入:返回值：%s,User:%S",insert,user));


        parameter.setUsername("李四");
        int update = sqlSession.update("com.azzcs.dao.UserMapper.update", parameter);
        user = sqlSession.selectOne("com.azzcs.dao.UserMapper.selectOne", parameter);
        System.out.println(String.format("执行修改:返回值：%s,User:%S",update,user));

        int delete = sqlSession.delete("com.azzcs.dao.UserMapper.delete", parameter);
        user = sqlSession.selectOne("com.azzcs.dao.UserMapper.selectOne", parameter);
        System.out.println(String.format("执行删除:返回值：%s,User:%S",delete,user));
    }

    @Test
    public void mapperTest() {
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User parameter =  User.builder().id(1).build();

        mapper.delete(parameter);
        User user = mapper.selectOne(parameter);
        System.out.println("开始前User:"+user);

        parameter.setUsername("张三");
        int insert = mapper.insert(parameter);
        user = mapper.selectOne(parameter);
        System.out.println(String.format("执行插入:返回值：%s,User:%S",insert,user));

        parameter.setUsername("李四");
        int update = mapper.update(parameter);
        user = mapper.selectOne(parameter);
        System.out.println(String.format("执行修改:返回值：%s,User:%S",update,user));


        int delete = mapper.delete(parameter);
        user = mapper.selectOne(parameter);
        System.out.println(String.format("执行删除:返回值：%s,User:%S",delete,user));
    }

}
